package com.home.qh.dao.hibernate;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.home.qh.dao.ExchangeRateDao;
import com.home.qh.model.ExchangeRate;


/**
 * This class interacts with Spring's HibernateTemplate to save/delete and
 * retrieve Role objects.
 *
 * @author <a href="mailto:bwnoll@gmail.com">Bryan Noll</a> 
 */
@Repository
public class ExchangeRateDaoHibernate extends GenericDaoHibernate<ExchangeRate, Long> implements ExchangeRateDao {

    /**
     * Constructor to create a Generics-based version using ExchangeRate as the entity
     */
    public ExchangeRateDaoHibernate() {
        super(ExchangeRate.class);
    }

    /**
     * {@inheritDoc}
     */
    public List<ExchangeRate> getExchangeRateByAppDate(String fromDate, String toDate){
    	DetachedCriteria criteria = DetachedCriteria.forClass(ExchangeRate.class);

        if (fromDate != null) {
        	criteria.add(Restrictions.ge("appDate", fromDate));
		}
        
        if (toDate != null) {        	
        	criteria.add(Restrictions.le("appDate", toDate));
		}
    	
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        
        return getHibernateTemplate().findByCriteria(criteria);
    }
    
    public List<ExchangeRate> getExchangeRateByExchangeRate(ExchangeRate exchangeRate, String sort, boolean desc, int startResult, int maxResults) {
        DetachedCriteria criteria = DetachedCriteria.forClass(ExchangeRate.class);

        if (exchangeRate.getId() != null) {
        	criteria.add(Restrictions.eq("id", exchangeRate.getId()));
		}
              
        if(sort != null && !"".equalsIgnoreCase(sort)) {
            if(desc) criteria.addOrder(Order.desc(sort));
            else criteria.addOrder(Order.asc(sort));
        }

        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        
        return getHibernateTemplate().findByCriteria(criteria, startResult, maxResults);
	}

   
}
